#include <key.h>
#include <motor.h>

//key主线程
static void thread_key_entry(void* parameter)
{
    static int door_close_old = 0;

    while (1)
    {
        if(LIMIT_UP || LIMIT_DOWN)
        {
            motor_stop();
        }

        if(KEY1) 
        {
            motor_open();
        }

        if(KEY2) 
        {
            motor_close();
        }

        if(door_close_old == 0 &&  DOOR_CLOSE)
        {
            motor_close();
        }
        door_close_old = DOOR_CLOSE;


        rt_thread_mdelay(20);
    }
}


void key_init(void)
{
	//输入
    rt_pin_mode(PIN_KEY1, PIN_MODE_INPUT);
    rt_pin_mode(PIN_KEY2, PIN_MODE_INPUT);

    rt_pin_mode(PIN_DOOR_CLOSE, PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_LIMIT_UP  , PIN_MODE_INPUT_PULLDOWN);
    rt_pin_mode(PIN_LIMIT_DOWN, PIN_MODE_INPUT_PULLDOWN);

    /* 创建key线程 */
    rt_thread_t tid = rt_thread_create("key",thread_key_entry,RT_NULL,1024,4,10);
    if (tid != RT_NULL) rt_thread_startup(tid);  
}
